/**
 * 
 */
package com.ffcs.crmd.tsp.server.netty.processor;

import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.ffcs.crmd.tsp.api.dto.client.TspJobIdentityInfo;
import com.ffcs.crmd.tsp.api.dto.netty.RemotingCommand;
import com.ffcs.crmd.tsp.api.dto.page.PageInfo;
import com.ffcs.crmd.tsp.api.dto.page.RetVo;
import com.ffcs.crmd.tsp.api.netty.INettyProcessor;
import com.ffcs.crmd.tsp.common.constant.LoggerName;
import com.ffcs.crmd.tsp.common.netty.ResponseCode;
import com.ffcs.crmd.tsp.core.util.BeanUtils;
import com.ffcs.crmd.tsp.core.util.XmlUtil;
import com.ffcs.crmd.tsp.server.core.DataBaseHandler;
import com.ffcs.crmd.tsp.server.core.RemotingHandler;
import com.ffcs.crmd.tsp.server.jdbc.entity.JobIdentityInfo;

import io.netty.channel.ChannelHandlerContext;

/**
 * 功能说明:
 *
 * @author ZHONGFUHUA
 * 
 * @since 
 *
 */
public class QueryJobIdentityInfoProcessor implements INettyProcessor {
    
    /**
     * 日志
     */
    private final static Logger LOG = LoggerFactory.getLogger(LoggerName.RemotingLoggerName);
    
    /**
     * 任务数据库服务
     */
    private DataBaseHandler     dataBaseHandler;
    
    /**
     * 功能说明:
     * 
     * @param remotingController
     */
    public QueryJobIdentityInfoProcessor(RemotingHandler remotingController, DataBaseHandler dataBaseHandler) {
        this.dataBaseHandler = dataBaseHandler;
    }
    
    /**
     * 功能说明:
     * 
     * @param ctx
     * @param request
     * @return
     */
    @Override
    public RemotingCommand processRequest(ChannelHandlerContext ctx, RemotingCommand request) {
        RemotingCommand response = RemotingCommand.createResponseCommand(ResponseCode.SUCCESS);
        String requestXml = new String(request.getBody());
        try {
            LOG.debug("SERVER-NETTY: one request to query job identity info is received that body=[{}]", requestXml);
            TspJobIdentityInfo tspJobIdentityInfoVO = (TspJobIdentityInfo) XmlUtil.fromXml(requestXml, TspJobIdentityInfo.class);
            RetVo<TspJobIdentityInfo> tspJobIdentityInfoRetVo = new RetVo<TspJobIdentityInfo>();
            Integer pageNumber = tspJobIdentityInfoVO.getPageNumber();
            Integer pageSize = tspJobIdentityInfoVO.getPageSize();
            
            JobIdentityInfo jobIdentityInfoParam = new JobIdentityInfo();
            BeanUtils.applyIf(jobIdentityInfoParam, tspJobIdentityInfoVO);
            
            if (pageNumber != null && pageSize != null) {
                PageInfo<JobIdentityInfo> jobIdentityInfoPageInfo = dataBaseHandler.getServiceSupport()
                    .queryJobIdentityInfoPage(jobIdentityInfoParam);
                PageInfo<TspJobIdentityInfo> tspJobIdentityInfoPageInfo = new PageInfo<TspJobIdentityInfo>();
                List<TspJobIdentityInfo> tspJobIdentityInfoList = BeanUtils.copyList(jobIdentityInfoPageInfo.getList(),
                    TspJobIdentityInfo.class);
                
                BeanUtils.applyIf(tspJobIdentityInfoPageInfo, jobIdentityInfoPageInfo);
                tspJobIdentityInfoPageInfo.setList(tspJobIdentityInfoList);
                tspJobIdentityInfoRetVo.setPageInfo(tspJobIdentityInfoPageInfo);
            } else if (tspJobIdentityInfoVO.getId() != null) {
                TspJobIdentityInfo tspJobIdentityInfo = new TspJobIdentityInfo();
                JobIdentityInfo jobIdentityInfo = dataBaseHandler.getServiceSupport().getJobIdentityInfo(jobIdentityInfoParam);
                BeanUtils.applyIf(tspJobIdentityInfo, jobIdentityInfo);
                tspJobIdentityInfoRetVo.setObject(tspJobIdentityInfo);
            } else {
                List<JobIdentityInfo> jobIdentityInfoList = dataBaseHandler.getServiceSupport().queryJobIdentityInfo(jobIdentityInfoParam);
                List<TspJobIdentityInfo> tspJobIdentityInfoList = BeanUtils.copyList(jobIdentityInfoList, TspJobIdentityInfo.class);
                tspJobIdentityInfoRetVo.setDataList(tspJobIdentityInfoList);
            }
            response.setBody(XmlUtil.toXml(tspJobIdentityInfoRetVo).getBytes());
        } catch (Exception e) {
            LOG.error("SERVER-NETTY: query job identity info[{}] exception:", requestXml, e);
            String responseBody = String.format("query job identity info:%s", e.getMessage());
            response.setCode(ResponseCode.FAIL);
            response.setBody(responseBody.getBytes());
        }
        return response;
    }
    
}
